const app = getApp();
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tid:20123,
    info:[],
    xgxx:[],
    plnum:0,
    weburl:'',
    no:false,
    inputStatus:false,
    fabu:false,
    pinglun:'',
    pllist:[],
    is_zan:false,
    keyword:[],
    toView:'default',
    windowHeight:'',
    pinglunts:'优质的评论将优先展示',
    pinglunbtn:'发布',
    lid:0,
    hfindex:0,
    pldisplaynum:2,
    pldisplaynumtishi:'查看全部回复',
    pltotal:3,
    pltotaltishi:'查看完整评论',
    XgwdList:[],
    url:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  chakanpinglun:function () {
    if (this.data.pltotal == 3) {
      this.setData({
        pltotal:this.data.pllist.length,
        pltotaltishi:'收起评论'
      })
    }else{
      this.setData({
        pltotal:3,
        pltotaltishi:'查看完整评论'
      })      
    }
  },
  onLoad: function (options) {
    var that = this;
    this.setData({
      tid:options.tid,
      type:options.type
    })
    
    // this.setData({
    //   url:'https://m.feijiu.net/toutiao/article/'+options.tid+'.html'
    // })

    console.log(this.data.url)

    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (app.checkFjLogin()) {
      var api_input = {
        // tid:20123,
        tid:options.tid,
        UID:FjLoginLog.UID
      }
    }else{
      var api_input = {
        // tid:20123
        tid:options.tid
      }
    } 

    app.getDaoPost('api/FeijiuApp/Post_FJ_GetToutiaoInfo',api_input,function (r) {
      if (r.code == 200) {
        
        r.data.info.Title = app.strOtherDiscode(r.data.info.Title);
        r.data.info.AddTime = r.data.info.AddTime.replace('T'," ").substring(5,16);
        if (r.data.info.Photo) {
          r.data.info.Photo = app.photoSplitOne(r.data.info.Photo)
        }
        if (r.data.info.Keyword) {
          r.data.info.Keyword = r.data.info.Keyword.split('、');
        }
        
        // var dd = r.data.info.Messages.replace(/<\/?.+?>/g,"");
        var dd = r.data.info.Messages
        WxParse.wxParse('dd', 'html', dd, that, 5);
        r.data.info.Messages = dd;

        var xgxxx = r.data.Xgxx;
        for (var i = 0; i < xgxxx.length; i++) {
          xgxxx[i].title = app.strOtherDiscode(xgxxx[i].title);
        }

        that.setData({
          xgxx:r.data.Xgxx,
          info:r.data.info,
          plnum:r.data.plnum,
          weburl:r.data.url,
          XgwdList:r.data.XgwdList
        })
      }
    })
    this.AddTtHit();
    wx.getSystemInfo({
      success:function (res) {
        that.setData({
          windowHeight:res.windowHeight+'px'
        })
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var news_zan = wx.getStorageSync('news_zan');  
    if (news_zan) {
      if (news_zan.includes(this.data.tid)) {
        this.setData({
          is_zan:true
        })
      }
    }  
    this.getpinglun();    
  },
  towenda:function (e) {
    wx.navigateTo({
      url: '../wenda/detaile?tqid='+e.currentTarget.dataset.tqid
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    var shareObj = {
      title: this.data.info.Title,
      path: '/pages/news/newsinfo?tid='+this.data.tid,
      imgUrl: '',
    }
    return shareObj;
  },
  toinfo:function (e) {
    wx.navigateTo({
      url: 'newsinfo?tid='+e.currentTarget.dataset.tid
    })  
  },
  toinfo2:function (e) {
    wx.navigateTo({
      url: 'newsinfo?tid='+e.currentTarget.dataset.tid+ '&type=' + e.currentTarget.dataset.type
    })  
  },
  AddTtHit:function  () {
     var api_input = {
        tid:this.data.tid,
        strRSA:app.data.strRSA
     }
    app.getDaoPostOld('api/FeijiuApp/AddTtHit',api_input,function (r) {

    })    
  },
  AddTtZan:function () {
     var that = this;
     var api_input = {
        tid:this.data.tid,
        strRSA:app.data.strRSA
     }
    app.getDaoPostOld('api/FeijiuApp/AddTtZan',api_input,function (r) {      
      if (r.code == 200) {
        app.tishi('点赞成功!');
        var news_zan = wx.getStorageSync('news_zan');
        if (!news_zan) {
          news_zan = new Array();
        }
        news_zan.push(that.data.tid);
        wx.setStorageSync('news_zan',news_zan);
        that.setData({
          is_zan:true
        })
      }
    }) 
  },
  changeInput:function () {
    this.setData({
      inputStatus:true
    })
  },
  keyboardchang:function (e) {   
    if (e.detail.height == 0) {
      this.setData({
        inputStatus:false
      })      
    }
  },
  bindTextAreaInput:function (e) {
    if (e.detail.value.length > 0) {
      this.setData({
        fabu:true
      })
    }else{
      this.setData({
        fabu:false
      })
    }
    this.setData({
      pinglun:e.detail.value
    })
  },
  fabuBtn:function (e) {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (app.checkFjLogin()) {
      var api_input = {
        tid:this.data.tid,
        lid:this.data.lid,
        userid:FjLoginLog.UID,
        username:FjLoginLog.UName,
        vcontent:this.data.pinglun,
        strRSA:app.data.strRSA
      }
      app.getDaoPostOld('api/FeijiuApp/AddTtPlHf',api_input,function (r) {
        if (r.code == 200) { 
          if (api_input.lid == 0) {
            var pop = {
              addtime: r.data.addtime.replace('T', " ").substring(0,19),
              context: r.data.cont,
              huifu: "[]",
              lid: r.data.lid,
              pic: r.data.userimage,
              uid: 0,
              xm: r.data.username           
            }
            var pllist1 = that.data.pllist;
             pllist1.unshift(pop);
             app.tishi('评论成功!');
             that.setData({
                inputStatus:false,
                pllist:pllist1,
                pinglun:'',
                plnum:pllist1.length
            }) 
          }else{
            var hf = {
              addtime:app.dateformatT(r.data.addtime,16),
              hid:r.data.lid,
              info:that.data.pinglun,
              uname:r.data.username
            } 
            var pllist1 = that.data.pllist;
            pllist1[that.data.hfindex].huifu.unshift(hf);           
            var huifudata = 'pllist['+that.data.hfindex+'].huifu';
            app.tishi('回复成功!');
            that.setData({
              lid:0,
              [huifudata]:pllist1[that.data.hfindex].huifu,
              pinglunts:'优质的评论将优先展示',
              pinglunbtn:'发布', 
              inputStatus:false,              
            })
          }         

        }else{
          this.setData({
            inputStatus:false,
            lid:0,
            pinglunts:'优质的评论将优先展示',
            pinglunbtn:'发布'            
          })
          app.tishi(r.message);
        }
      })
    }else{
      wx.navigateTo({ url: '../../pages/login/index' }); 
      return false
    }




  },
  fabuBtnNo:function () {
    
  },
  inputover:function () {
    this.setData({
      inputStatus:false
    })
  },
  getpinglun:function () {

    // api/FeijiuApp/GetToutiaoPinglun?TID={TID}&strRSA={strRSA}
      var that = this;
      var url = "api/FeijiuApp/GetToutiaoPinglun?TID=" + this.data.tid + "&strRSA=";
      app.getDaoGet(url, function (r){
        // for (var i = 0; i < r.data.length; i++) {
        //   r.data[i].addtime = r.data[i].addtime.replace('T', " ").substring(0,19)
        // }
        if (r.data.length > 0) {
          for (var i = 0; i < r.data.length; i++) {
            r.data[i].addtime = app.dateformatT(r.data[i].addtime,16);
            if (typeof(r.data[i].huifu) == 'string') {
              r.data[i].huifu = JSON.parse(r.data[i].huifu);
              if (r.data[i].huifu.length > 0) {
                for (var j = 0; j < r.data[i].huifu.length; j++) {
                  r.data[i].huifu[j].addtime = app.dateformatT(r.data[i].huifu[j].addtime,16);
                  // r.data.plList[i].huifu[j].addtime = 123;
                  // console.log(r.data.plList[i].huifu[j].addtime)
                }
              }

            }
          }
        }
 
        if (r.code == 200) {
          that.setData({
            pllist:r.data
          })
        }
      })
  },
  removearr:function(arr,val) {  
    var index = arr.indexOf(val);  
    if (index > -1) {  
      arr.splice(index, 1);  
    } 
    return arr; 
  },
  tojump:function (e) {
      this.setData({
        toView:'jump'
      })
  },
  huifu:function (e) {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (app.checkFjLogin()) {
       var lid = e.currentTarget.dataset.plid;
       var name = e.currentTarget.dataset.name;
       this.setData({
        inputStatus:true,
        pinglunts:'回复 '+name,
        pinglunbtn:'回复',
        lid:lid,
        hfindex:e.currentTarget.dataset.inx
       })
    }else{
      wx.navigateTo({ url: '../../pages/login/index' }); 
      return false      
    }    
   

  },
  plmore:function (e) {
    var idx = e.currentTarget.dataset.idx;
    if (this.data.pldisplaynum == 2) {
      this.setData({
        pldisplaynum:this.data.pllist[idx].huifu.length,
        pldisplaynumtishi:'收起回复'
      })      
    }else{
      this.setData({
        pldisplaynum:2,
        pldisplaynumtishi:'查看完整回复'
      })      
    }


  },

  toSearch:function (e) {
    wx.navigateTo({
      url: 'search?key='+e.currentTarget.dataset.key
    })
  }

})